package com.example.android427;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.android427.Fragment.qikanFragment1;
import com.example.android427.Fragment.qikanFragment2;
import com.example.android427.Fragment.qikanFragment3;

import java.util.ArrayList;
import java.util.List;

public class qikanActivity extends AppCompatActivity implements View.OnClickListener{
    private ViewPager mviewpager;
    private FragmentPagerAdapter mAdpter; //适配器
    private List<Fragment> mFragments; //数据源

    private LinearLayout tuijian;//推荐
    private LinearLayout fenlei;//分类
    private LinearLayout wenzhang;//文章

    private TextView tuijia;
    private TextView fenle;
    private TextView wenzhan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qikanactivity_main);

        ImageButton qk_b1 = (ImageButton) findViewById(R.id.qk_b1);
        qk_b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(qikanActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });



        initView();//初始化控件
        initEvents();//为四个LinearLayout添加点击事件
        setSelect(0);  //让第一个fragment进行显示


    }

    private void initEvents() {  //为三个按钮设置点击事件
        tuijian.setOnClickListener(this);
        fenlei.setOnClickListener(this);
        wenzhang.setOnClickListener(this);
    }

    private void initView() {  //初始化
        mviewpager = (ViewPager) findViewById(R.id.id_content);//关联viewpager

        tuijian = (LinearLayout) findViewById(R.id.tuijian);//初始化LinearLayout
        fenlei = (LinearLayout) findViewById(R.id.fenlei);
        wenzhang = (LinearLayout) findViewById(R.id.wenzhang);

        tuijia =findViewById(R.id.t1);//初始化图标按钮
        fenle = findViewById(R.id.t2);
        wenzhan = findViewById(R.id.t3);


        mFragments = new ArrayList<Fragment>();  //初始化数据源
        Fragment shouye1 = new qikanFragment1();
        Fragment shujia1 = new qikanFragment2();
        Fragment geren1 = new qikanFragment3();

        mFragments.add(shouye1);
        mFragments.add(shujia1);
        mFragments.add(geren1);


        mAdpter = new FragmentPagerAdapter(getSupportFragmentManager()) { //设置适配器
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }//返回不同的fragment
            @Override
            public int getCount() {
                return mFragments.size();
            }//返回数据源的数量
        };
        mviewpager.setAdapter(mAdpter);
        mviewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                int currentItem = mviewpager.getCurrentItem();
                setTab(currentItem);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void setSelect(int i) {
        setTab(i);
        mviewpager.setCurrentItem(i);
    }

    private void setTab(int i) {  //切换图片
        resetImgs();
        switch (i) {
            case 0:
                tuijia.setTextColor(getResources().getColor(R.color.colorTextViewPres));
                break;
            case 1:
                fenle.setTextColor(getResources().getColor(R.color.colorTextViewPres));
                break;
            case 2:
                wenzhan.setTextColor(getResources().getColor(R.color.colorTextViewPres));
                break;
            default:
                break;
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tuijian:
                setSelect(0);
                break;
            case R.id.fenlei:
                setSelect(1);
                break;
            case R.id.wenzhang:
                setSelect(2);
                break;
        }
    }

    //切换图片至暗色
    private void resetImgs() {
        tuijia.setTextColor(getResources().getColor(R.color.colorTextViewNorma));
        fenle.setTextColor(getResources().getColor(R.color.colorTextViewNorma));
        wenzhan.setTextColor(getResources().getColor(R.color.colorTextViewNorma));
    }
}


